package com.wanmait.translate.configuration.listener;

import com.wanmait.translate.init.service.impl.InitAuditSericeImpl;
import com.wanmait.translate.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;

/**
 * @Author huweibin
 * @Description Spring容器启动监听
 * @Date 14:38 2020/11/23
 **/
@Component
public class SpringApplicationStartedListener implements ApplicationListener<ContextRefreshedEvent> {
    private static final Logger LOG= LoggerFactory.getLogger(SpringApplicationStartedListener.class);
    @Resource
    private InitAuditSericeImpl initAuditSerice;


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        //防止重复执行。
        if (event.getApplicationContext().getParent() ==  null ){
            LOG.info("Sping成功启动，开始执行缓存初始化。。。。。。。");
            //将最近的已审核未审核数更新到缓存
            try {
                initAuditSerice.initToDo(DateUtil.getNowDate());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            LOG.info("审核情况已更新到缓存。。。。。");
        }
    }

    }


